﻿using System;
using System.Reflection;

namespace Raider.WinForms.Actions.Linkage
{
    /// <summary>
    /// Marks the action's property as linkable with the property of some client control or component.
    /// </summary>
    [Serializable]
    [AttributeUsage(AttributeTargets.Property)]
    public class LinkWithPropertyAttribute : LinkAttribute
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="LinkWithPropertyAttribute" /> class.
        /// </summary>
        /// <param name="clientProperty">Name of the property of the client controls and components to link with.</param>
        /// <param name="clientTypes">Supported types of the client controls and components.</param>
        public LinkWithPropertyAttribute(string clientProperty, params Type[] clientTypes)
            : base(clientTypes)
        {
            ClientProperty = clientProperty;
        }

        /// <summary>
        /// Property name of the client components to link with.
        /// </summary>
        public string ClientProperty
        {
            get;
            private set;
        }

        /// <summary>
        /// Creates Linker object that can be used to link properties of the real objects.
        /// </summary>
        /// <param name="actionMember">Info about the applicable property.</param>
        public override ActionMemberLinker CreateLinker(MemberInfo actionMember)
        {
            return new ActionPropertyLinker((PropertyInfo)actionMember, ClientProperty, ClientTypes);
        }

        /// <summary>
        /// Returns an array of the <see cref="LinkWithPropertyAttribute"/> instances applied to the
        /// specified class member.
        /// </summary>
        public static LinkWithPropertyAttribute[] GetAttributes(MemberInfo element)
        {
            return (LinkWithPropertyAttribute[])GetCustomAttributes(element, 
                typeof(LinkWithPropertyAttribute), true);
        }
    }
}
